********************************************************************************
*** ITT Renewals 

use "data/cleaned/collapsed action with credit", clear
	merge 1:1 id using "data/cleaned/hugo/hugo anyreg het", assert(3) nogen

*replace bound 1 to 100 for ITT graphs
foreach var of varlist bound anybound* threemonther threemonthcrer {
	replace `var' = 100 if `var'==1
}

* Generate some alternative take-up variables 
gen bound_renew = bound
	replace bound_renew = 0 if daily==1 & missing(paydays2) // reclassify those who do not top up their days
	replace bound_renew = 0 if daily==0 & !missing(day_canceled) // reclassify canceled control users
bysort daily: tab bound bound_renew, row mi

gen anybound_renew = anybound7 
	replace anybound_renew = 0 if daily==1 & missing(paydays2) & bound==0 
		// reclassify Hugo bound users who don't top up
	replace anybound_renew = 0 if daily==0 & !missing(day_canceled) 
		// reclassify canceled control users
bysort daily: tab anybound7 anybound_renew, row mi


* "two-thirds of pay-as-you-go contract enrollees make a second purchase."
sum bound_renew if daily==1 & bound==100
di round(`r(mean)',.1) " two-thirds of pay-as-you-go contract enrollees make a second purchase."



*** hugo and any insurance outcomes
* save regression output and reshape to present means as bar charts
* save confidence interval of coefficient for mean of treatment group in bar charts
local outcomes "bound bound_renew anybound7 anybound_renew"
keep id daily `outcomes'

foreach var of varlist `outcomes' {
	reg `var' daily, r		
}

local i=1		
foreach var of varlist `outcomes' {
	eststo est`i':  quietly reg `var' daily, r
	quietly summ `var'
	estadd scalar mean = r(mean)
	local i=`i'+1
}

esttab est1 est2 est3 est4 using "output/tables/ITT daily contract renewed.tex", ///
prehead("\begin{tabular}{l*{5}{c}} \hline\hline") ///
replace label nomtitle unstack nonumbers nolines ///
stats(N mean, fmt(%12.3gc) labels("N" "Mean")) ///
prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.9mm}"))) /// 
	mgroups("Take-Up (Hugo)" "Take-Up \& Renewal (Hugo)" "Take-Up (Any)" "Take-Up \& Renewal (Any)", ///
		pattern(1 1 1 1))


foreach var of varlist `outcomes' {
	rename `var' val`var'
}
reshape long val, i(id daily) j(type) string

gen lcl = .
gen ucl = .
foreach outcome of local outcomes {
	reg val daily if type=="`outcome'", r
	local lcl = _b[_cons] + _b[daily] - invttail(e(df_r), .025) * _se[daily]
	local ucl = _b[_cons] + _b[daily] + invttail(e(df_r), .025) * _se[daily]
	replace lcl = `lcl' if type=="`outcome'" & daily==1
	replace ucl = `ucl' if type=="`outcome'" & daily==1
}

collapse (mean) meanval = val (sd) sdval = val (count) n=val, by(daily type lcl ucl)

gen numtype = .
replace numtype = 1 if type == "anybound7"
replace numtype = 2 if type == "anybound_renew"

gen dtype = .
replace dtype = daily if numtype == 1
replace dtype = daily + 3 if numtype == 2

local j=1
foreach outcome of local outcomes {
	forval i=0/1 {
		summ meanval if type=="`outcome'" & daily==`i'
		local m`j' = round(`r(mean)',.01)
		if `m`j''>10 local m`j' = round(`m`j'',.1)
		local m`j'_di: di %9.1f `m`j''
		di "`m`j'_di'"
		local j=`j'+1
	}
}

twoway 	(bar meanval dtype if daily == 0 & !mi(numtype), ///
			text(`=`m5'+.1' -.15 "`m5_di'", place(n)) ///
			text(`=`m7'+.1' 2.85 "`m7_di'", place(n))) /// 
		(bar meanval dtype if daily == 1 & !mi(numtype), ///
			text(`=`m6'+.1' 1.1 "`m6_di'", place(n)) ///
			text(`=`m8'+.1' 4.1 "`m8_di'", place(n))) ///
		(rcap lcl ucl dtype if !mi(numtype), col("185 217 235")), ///
			ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%") ///
			xlabel(0.5 "Take-Up" 3.5 "Take-Up & Renewal") ///
			ytitle("") xtitle("") ///
			legend(size(med) order(1 "Traditional" 2 "Pay-As-You-Go" 3 "95% CI") pos(1) ring(0)) ///
			subtitle("Take-Up through Any Carrier (%)", position(11) span)
graph save two.gph, replace

replace numtype = .
replace numtype = 1 if type == "bound"
replace numtype = 2 if type == "bound_renew"

replace dtype = .
replace dtype = daily if numtype == 1
replace dtype = daily + 3 if numtype == 2

twoway 	(bar meanval dtype if daily == 0, ///
			text(`=`m1'+.1' -.15 "`m1_di'", place(n)) ///
			text(`=`m3'+.1' 2.85 "`m3_di'", place(n))) ///
		(bar meanval dtype if daily == 1, ///
			text(`=`m2'+.1' 1.1 "`m2_di'", place(n)) ///
			text(`=`m4'+.1' 4.1 "`m4_di'", place(n))) ///
		(rcap lcl ucl dtype, col("185 217 235")), ///
			ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%") ///
		legend(off) ///
		xlabel(0.5 "Take-Up" 3.5 "Take-Up & Renewal") ytitle("") xtitle("") ///
		subtitle("Take-Up through Experiment (%)", position(11) span)
graph save one.gph, replace
		
graph combine one.gph two.gph, iscale(0.9) imargin(medsmall)
graph export "output/figures/ITT daily contract bars renewed.pdf", replace

erase one.gph
erase two.gph
